import { http } from "@/api/http/request";
import type {
  LoginData,
  LoginSmsData,
  LoginScanData,
  UserInfo,
  CreateUserParams,
  RegisterParams,
  CaptchaParams,
} from "@/api/types/user";
import { API_URLS } from "../constants/api-url";

// import user from "@/mock/data/json/user.json";
/**
 * 用户登录
 * @param data 登录表单数据
 */
export const login = (data: LoginData) => {
  return http.post<{ token: string }>(API_URLS.LOGIN, data);
};
/**
 * 密码登录
 * @param data 登录表单数据
 */
export const passwordLogin = (data: LoginData) => {
  // return {token: user.token,refreshToken: user.refreshToken,user: user.userInfo };// 先使用模拟数据

  return http.post<{ token: string; refreshToken: string; user: UserInfo }>(
    API_URLS.PASSWORD_LOGIN,
    data,
    {
      skipAuth: true,
    }
  );
};
/**
 * 短信登录
 * @param data 登录表单数据
 */
export const smsLogin = (data: LoginSmsData) => {
  return http.post<{ token: string; refreshToken: string; user: UserInfo }>(
    API_URLS.SMS_LOGIN,
    data
  );
};
/**
 * 扫码登录
 * @param data 登录表单数据
 */
export const checkQRStatus = (data: LoginScanData) => {
  return http.post<{
    token: string;
    refreshToken: string;
    user: UserInfo;
    status: number | string;
  }>(API_URLS.CHECK_QR_STATUS, data);
};
/**
 * 用户登出
 */
export const logout = () => {
  return http.post(API_URLS.LOGOUT);
};

/**
 * 获取用户列表
 */
export const getUserList = (params?: any) => {
  console.log("获取用户列表参数：", params);
  try {
    return http.get<UserInfo[]>(API_URLS.GET_USER_LIST, { params });
  } catch (error) {
    console.log(error);
  }
};
/**
 * 管理员获取指定用户信息
 */
export const getAdminUserInfo = (id?: string) => {
  return http.get<{
    userInfo: UserInfo;
    roles: string[];
    permissions: string[];
  }>(API_URLS.GET_USER_INFO);
};
/**
 * 获取用户个人信息
 */
export const getCurrUserInfo = (id?: string) => {
  return http.get(API_URLS.GET_USER_INFO);
};
/**
 * 第二次请求：获取权限和角色（按需调用）
 */
export const getUserPermissions = () => {
  return http.get<{
    roles: string[];
    permissions: string[];
  }>(API_URLS.GET_USER_ROLE_LIST);
};

/**
 * 删除指定用户
 */

export const deleteUser = (id: string) => {
  return http.post(API_URLS.DELETE_USER, { id });
};

/**
 * 批量删除用户
 */
export const batchDeleteUsers = (ids: string[]) => {
  return http.post(API_URLS.BATCH_DELETE_USER, ids);
};
/**
 * 创建新用户
 */
export const createUser = (data: CreateUserParams) => {
  return http.post(API_URLS.CREATE_USER, data);
};

/**
 * 更新用户信息
 * @param data 用户信息
 */
export const updateUser = (id: string, data: CreateUserParams) => {
  return http.put(API_URLS.UPDATE_USER + `/${id}`, data);
};
/**
 * 用户注册
 * @param data 注册参数
 */
export const register = (data: RegisterParams) => {
  return http.post(API_URLS.REGISTER, data, { skipAuth: true });
};

/**
 * 发送注册验证码
 * @param data 手机号
 */
export const sendCaptcha = (data: CaptchaParams) => {
  return http.post(API_URLS.SEND_CAPTCHA, data, { skipAuth: true });
};
